import { DialogService } from 'ng-devui';
import { UserService } from 'src/app/@core/services/user/user.service';
import { JobOrderService } from 'src/app/@core/services/jobOrder/job-order.service';
import { Component } from '@angular/core';
import { FooterItems } from 'src/app/@core/data/job';
import { ActivatedRoute, Router } from '@angular/router';
import { PersonalizeService } from 'src/app/@core/services/personalize.service';
import { ReportHistoriesComponent } from 'src/app/@shared/components/report-histories/report-histories.component';
import { environment } from 'src/environments/environment';
@Component({
  selector: 'app-opterator-tasks-list',
  templateUrl: './opterator-tasks-list.component.html',
  styleUrls: ['./opterator-tasks-list.component.scss']
})
export class OpteratorTasksListComponent {
  selectedTab = '全部'
  userInfo :any
  tabs = ['全部', '未开工', '已开工', '已完成','暂停']
  employeeCode: any
  name: any
  data=[]
  headUrl:any
  jobNumber:any = ""
  departname:any=""
  now:any
  selectedFooterItem = FooterItems.TaskList
  footerItems:FooterItems[] = Object.values(FooterItems)
  dataNumer: any=[]
  constructor( private JobOrderService: JobOrderService,private router: Router, private route: ActivatedRoute, private personalizeService: PersonalizeService,private UserService:UserService,private DialogService:DialogService
  ){
    this.employeeCode = JSON.parse(localStorage.getItem('userinfo')).employeeCode;
    this.name = JSON.parse(localStorage.getItem('userinfo')).
    username;
    this.headUrl = environment.modifiedApiUrl+JSON.parse(localStorage.getItem('userinfo')).headUrl;
    this.personalizeService.initTheme();
  }
  ngOnInit(): void {
    this.UserService.GetUserByEmployeeCode(this.employeeCode).subscribe((res:any)=>{
      this.jobNumber = res.jobNumber
      this.UserService.GetDepartmentById(res.departmentId).subscribe((res:any)=>{
        this.departname = res.name
      })
    })
    this.JobOrderService.GetUserJobOrderProcessesByUserCode(this.employeeCode,"").subscribe((res:any)=>{
      this.data = res.item1
      this.dataNumer = res.item2
    })
    setInterval(() => {
      this.now = new Date();
    }, 1000);
  }
  detail(item:any){
    this.router.navigate(['/opterator-tasks/opterator-task-datail'], {
      relativeTo: this.route,
      queryParams: {jobOrderProcessId: item.jobOrderProcessId}
    });
  }
  selectTab(tab: string) {
    this.selectedTab = tab;
    const param = tab === '全部' ? '' : tab;
    this.JobOrderService.GetUserJobOrderProcessesByUserCode(this.employeeCode, param).subscribe((res: any) => {
      this.data = res.item1;
      this.dataNumer = res.item2;
    });
  }
  selectFooterItem(item: FooterItems) {
    this.selectedFooterItem = item;
  }
  reportHistories(dialogtype?:string){
    const results = this.DialogService.open({
      title: '报工历史',
      data:{
        data:{
          EmployeeCodeName:this.name,
          PageNumber: 1,
          PageSize: 20
        },
        title:'任务报工记录'
      },
      content: ReportHistoriesComponent,
      dialogtype: dialogtype,
      width: '800px',
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  }
  logout(dialogtype?:string){
    const results = this.DialogService.open({
      title: '退出登录',
      content: '确认退出登录？',
      width: '400px',
      dialogtype: dialogtype,
      onClose: () => {
      },
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            this.router.navigate(['/login']);
            results.modalInstance.hide();
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });

  }
  getStatus(tab: string) {
    switch (tab) {
      case '未开工':
        return 'warning';
      case '已开工':
        return 'info';
      case '已完成':
        return 'success';
      case '暂停':
        return 'waiting';
      default:
        return 'info';
    }
  }
  getStatusNumber(tab: string): any {
    if (tab === '全部') {
      // 将所有数值加起来
      return Object.values(this.dataNumer).reduce((acc: number, value: number) => acc + value, 0);
    }
    return this.dataNumer[tab] || 0;
  }
}
